<?
class Tbl_article extends CI_Model {
    var $tabel;

	 function __construct(){
         // Call the Model constructor
         parent::__construct();
		 $this->tabel = 'tbl_article';
	}
	
	//------------Main Function
	
	function add($judul, $isi, $kategori, $status, $tanggal, $penulis, $file_name) {
		
	$tanggal = "$tanggal ".date("H:i:s");
	$array = array(
		'id_article'   	=> "null",
		'id_artile_cat' => $kategori
		'title'    	   	=> $judul,
		'content'       => $isi,
		'kategori' 		=> $kategori,
		'status'   		=> $status,
		'tanggal'  		=> $tanggal,
		'penulis'  		=> $penulis,
		'file_name'		=> $file_name);
        $this->db->insert($this->tabel, $array);
    }
	
	function edit($id) {
      $query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE id='$id' AND kategori='Berita'");
      return $query->result();
	 }

	function edit_tokoh($id) {
      $query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE id='$id' AND kategori='Tokoh'");
      return $query->result();
	 }
	

	function edit_kisah_inspiratif($id) {
      $query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE id='$id' AND kategori='Kisah Inspiratif'");
      return $query->result();
	 }

	function delete($id) {
	 $this->db->query("DELETE FROM ".$this->tabel." WHERE id=$id");
    }
	
	function update($id, $judul, $isi, $kategori, $status, $tanggal, $file_name) {
	$set_data = "";
	if($file_name!=null)
		$set_data = ",file_name	='$file_name' ";
		
		$this->db->query("UPDATE ".$this->tabel." SET 
							isi		='$isi', 
							judul	='$judul',
							tanggal	='$tanggal', 
							kategori='$kategori',
							status	='$status'
							$set_data
							WHERE id=$id ");
    }
	
	//------------Function
	
	function update_status($id, $status_update){
		$this->db->query("UPDATE ".$this->tabel." SET 
							status	='$status_update'
							WHERE id=$id ");
    }
	
	function load_admin($data,$posisi) {
	$conditionQuery = "";
	if($data['deskripsi']!="null" || $data['deskripsi']!=null || $data['deskripsi']!=""){
		if($data['kategori']=="date"){
			$conditionQuery = $conditionQuery."AND date LIKE '";
			if($data['tahun']!="0")
				$conditionQuery = $conditionQuery.$data['tahun'];
			else
				$conditionQuery = $conditionQuery."____";
			$conditionQuery = $conditionQuery."-";
			if($data['bulan']!="0")
				$conditionQuery = $conditionQuery.$data['bulan'];
			else
				$conditionQuery = $conditionQuery."__";
			$conditionQuery = $conditionQuery."-";
			if($data['hari']!="0")
				$conditionQuery = $conditionQuery.$data['hari'];
			else
				$conditionQuery = $conditionQuery."__";
			$conditionQuery = $conditionQuery."%'";
		}
		elseif($data['deskripsi']!="null"){
			$conditionQuery = $conditionQuery."AND ".$data['kategori']." LIKE '%".$data['deskripsi']."%'";
		}
	}

	if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT *, DATE_FORMAT(tanggal, '%e %M %Y, %l:%i%p') as newdate FROM ".$this->tabel." WHERE kategori='".$data['kategori_artikel']."' $conditionQuery ORDER BY id DESC LIMIT $posisi,15");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT *, DATE_FORMAT(tanggal, '%e %M %Y, %l:%i%p') as newdate FROM ".$this->tabel." WHERE kategori='".$data['kategori_artikel']."' $conditionQuery ORDER BY id DESC LIMIT $posisi,15");
		return $query->result();
	 }
	
	function hitung_all($data){
		$conditionQuery = "";
		if($data['deskripsi']!="null" || $data['deskripsi']!=null || $data['deskripsi']!=""){
			if($data['kategori']=="date"){
				$conditionQuery = $conditionQuery."AND date LIKE '";
				if($data['tahun']!="0")
					$conditionQuery = $conditionQuery.$data['tahun'];
				else
					$conditionQuery = $conditionQuery."____";
				$conditionQuery = $conditionQuery."-";
				if($data['bulan']!="0")
					$conditionQuery = $conditionQuery.$data['bulan'];
				else
					$conditionQuery = $conditionQuery."__";
				$conditionQuery = $conditionQuery."-";
				if($data['hari']!="0")
					$conditionQuery = $conditionQuery.$data['hari'];
				else
					$conditionQuery = $conditionQuery."__";
				$conditionQuery = $conditionQuery."%'";
			}
			elseif($data['deskripsi']!="null"){
				$conditionQuery = $conditionQuery."AND ".$data['kategori']." LIKE '".$data['deskripsi']."'";
			}
		}
	
		$dataModel = $this->db->query("SELECT * FROM ".$this->tabel." WHERE kategori='".$data['kategori_artikel']."' ".$conditionQuery."");
		$banyak= $this->db->affected_rows($dataModel);
		return $banyak;
	}
		
	function guest_artikel(){
	  $posisi = $this->uri->segment(3);
    if(!is_numeric($posisi)){
      $posisi='0';
      $query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE status='publish' AND kategori='Berita' ORDER BY tanggal DESC LIMIT $posisi,4");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE status='publish' AND kategori='Berita' ORDER BY tanggal DESC LIMIT $posisi,4");
		return $query->result();
	}
	
	function guest_artikel_where($id){
		$query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE id=$id AND status='publish' AND kategori='Berita'");
		return $query->result();
	}
	
	
	function hitung_publish(){
		$data = $this->db->query("SELECT * FROM ".$this->tabel." WHERE status='publish' AND kategori='Berita'");
		$banyak= $this->db->affected_rows($data);
		return $banyak;
	}
	
	function getNewBerita(){
	 $query=$this->db->query("SELECT distinct *,DATE_FORMAT(tanggal, '%e %M %Y, %l:%i%p') as newDate FROM tbl_article WHERE kategori='Berita' ORDER BY tanggal DESC LIMIT 5");
     return $query->result();
	}
	
}
?>